﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace {{ Namespace }}.{{ DirName }}
{
    /// <summary>
    ///
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "root", "root" })]
    public class {{ Name }}AppService : IUserAppService
    {

        private readonly I{{ Namespace }}DbContext _dbContext;

        /// <summary>
        ///
        ///</summary>
        ///<param name="dbContext"></param>
        public {{ Name }}AppService(I{{ Namespace }}DbContext dbContext):base()
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 按页获取
        ///</summary>
        ///<param name="page">页码</param>
        ///<param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<{{ Name }}ListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.{{ Name }}.Where(t => 1 == 1).OrderByDescending(xy => xy.Id);
            var _pagedto = new PagedResultDto<{{ Name }}ListDto>();
            if (page == 1)
            {
                _pagedto.TotalCount = await query.CountAsync();
            }
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<{{ Name }}>, List<{{ Name }}ListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 根据ID获取单项
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public {{ Name }}Dto GetByIdAsync(int id)
        {
            var query = _dbContext.{{ Name }}.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new {{ Namespace }}Exception("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public {{ Name }}UpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.{{ Name }}.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new {{ Namespace }}Exception("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<{{ Name }}, {{ Name }}UpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<{{ Name }}Dto> CreateItemAsync({{ Name }}AddDto input)
        {
            var _userid = base.CurrentUserId();
            var newu = ObjectMapper.Map<{{ Name }}AddDto, {{ Name }}>(input);
            //添加自定义
            _dbContext.Add(newu);
            {% for item in ClassPropertys -%}
                {% if item.Name == "create_time" -%}
                    newu.create_time = DateTime.Now().ToUnixTimeSeconds();
                {% endif -%}
                {% if item.Name == "CreateDate" -%}
                    newu.CreateDate = DateTime.Now();
                {% endif -%}
                {% if item.Name == "CreateUserId" -%}
                    newu.CreateUserId = _userid;
                {% endif -%}
            {% endfor -%}
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(newu);
            return backinfo;
        }
        /// <summary>
        /// 更新一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<{{ Name }}Dto> UpdateItemAsync({{ Name }}UpdateDto input)
        {
            var info = await _dbContext.{{ Name }}.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new UserFriendlyException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<{{ Name }}UpdateDto, {{ Name }}>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(info);
            return backinfo;
        }
    }
}
